﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for RequestHelper
/// </summary>
public class RequestHelper
{
	public RequestHelper()
	{
		
	}

    public static string GetRequest(string sUrl, System.Web.HttpCookie cookie, string sAuthentication)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);

            // Pass the cookie
            if (cookie != null)
            {
                Cookie SecurityCookie = new Cookie(cookie.Name, cookie.Value);

                Uri uri = new Uri(sUrl);
                SecurityCookie.Domain = uri.Host;
                SecurityCookie.Expires = cookie.Expires;

                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;

                request.CookieContainer.Add(SecurityCookie);                
            }

            request.Headers.Add("Authorization: Basic " + sAuthentication);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

            string sRes = readStream.ReadToEnd();

            response.Close();

            return sRes;
        }
        catch { }

        return "";
    }
}
